//
//  YYYVisitorView.swift
//  HotNews
//
//  Created by YYY on 2017/9/25.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

//访客视图
class YYYVisitorView: UIView {

    //添加私有控件
    private lazy var iconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮罩控件
    private lazy var maskIconImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //房子
    private lazy var houseImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示标签
    private lazy var tipLabel = UILabel(text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
    
    //注册
    lazy var registerButton = UIButton(title: "注册", fontSize: 16, normalColor: UIColor.RGBColor(red: 20, green: 82, blue: 184), highlightedColor: UIColor.black, backgroundImageName: "common_button_white")
    //登录
    lazy var loginButton = UIButton(title: "登录", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white")
    
    //访客视图属性字典设置 imageName/message
    var visitorDictInfo: [String: String]?
    {
        didSet
        {
            guard let imageName = visitorDictInfo?["imageName"],
                let message = visitorDictInfo?["message"] else
            {
                return
            }
            tipLabel.text = message
            if imageName == ""
            {
                //设置首页转轮动画
                startAnimation()
                return
            }
            //不是首页隐藏房子、遮罩
            iconImageView.image = UIImage(named: imageName)
            houseImageView.isHidden = true
            maskIconImageView.isHidden = true
        }
    }
    
    //构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置界面
        setupUI()
    }

    //设置首页转轮动画
    private func startAnimation()
    {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        animation.toValue = 2 * Double.pi
        //完成后是否移除
        animation.isRemovedOnCompletion = false
        //将动画添加到图层
        iconImageView.layer.add(animation, forKey: nil)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension YYYVisitorView
{
    //设置界面
    private func setupUI()
    {
        backgroundColor = UIColor.RGBColor(red: 237, green: 237, blue: 237)
        
        //添加控件
        addSubview(iconImageView)
        addSubview(maskIconImageView)
        addSubview(houseImageView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //文本居中
        tipLabel.textAlignment = .center
        tipLabel.numberOfLines = 0
        
        //添加布局约束
        let margin: CGFloat = 20
        
        //加载视图
        iconImageView.center.x = center.x
        iconImageView.center.y = center.y - 60
        
         //房子视图
        houseImageView.center.x = iconImageView.center.x
        houseImageView.center.y = iconImageView.center.y
        
        //提示标签
        let tipLabelSize = tipLabel.text?.calculateLabelSize(fontSize: 14, size: CGSize(width: 250, height: CGFloat(MAXFLOAT))) ?? CGSize()
        
        tipLabel.frame = CGRect(origin: CGPoint(x: (kDeviceWidth-250)/2.0, y: iconImageView.bottom + margin), size: tipLabelSize)
        tipLabel.center.x = iconImageView.center.x

        //注册
        registerButton.setWidth(newWidth: 100)
        registerButton.setLeft(newLeft: tipLabel.left)
        registerButton.setTop(newTop: tipLabel.bottom + margin)
        
        //登录
        loginButton.setWidth(newWidth: registerButton.width)
        loginButton.setTop(newTop: registerButton.top)
        loginButton.setRight(newRight: tipLabel.right)
        
    }
}
